package com.news.servlet;

import com.news.db.OpDB;
import com.news.model.News;
import com.news.model.Person;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 添加新闻人物
 */
@WebServlet("/addperson")
public class AddPersonServlet extends HttpServlet {
    private boolean check(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        String person_name = request.getParameter("person_name");
        if (person_name == null || person_name.equals("")) {
            out.println("<script>alert('人物名字不能为空'); window.history.go(-1);</script>");
            return false;
        }

        String person_age = request.getParameter("person_age");
        if (person_age == null || person_age.equals("")) {
            out.println("<script>alert('人物年龄不能为空'); window.history.go(-1);</script>");
            return false;
        }

        String person_address = request.getParameter("person_address");
        if (person_address == null || person_address.equals("")) {
            out.println("<script>alert('人物住址不能为空'); window.history.go(-1);</script>");
            return false;
        }

        String person_job = request.getParameter("person_job");
        if (person_job == null || person_job.equals("")) {
            out.println("<script>alert('人物职业不能为空'); window.history.go(-1);</script>");
            return false;
        }

        String person_number = request.getParameter("person_number");
        if (person_number == null || person_number.equals("")) {
            out.println("<script>alert('人物票数不能为空'); window.history.go(-1);</script>");
            return false;
        }

        String person_content = request.getParameter("person_content");
        if (person_content == null || person_content.equals("")) {
            out.println("<script>alert('人物简介不能为空'); window.history.go(-1);</script>");
            return false;
        }

        return true;
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        if (check(request, response)) {
            String person_name = request.getParameter("person_name");
            int person_age = Integer.parseInt(request.getParameter("person_age"));
            String person_address = request.getParameter("person_address");
            String person_job = request.getParameter("person_job");
            String person_content = request.getParameter("person_content");
            int person_number = Integer.parseInt(request.getParameter("person_number"));
            Person person = new Person(person_name, person_age, person_job, person_address, person_content, person_number);
            PrintWriter out = response.getWriter();
            OpDB opdb = new OpDB();
            if (opdb.OpAddObject(person)) {
                out.println("<script>alert('新闻人物添加成功！'); window.location.href='http://localhost:8080/news_war_exploded/pageperson?rows=2&currentPage=1';</script>");
            } else {
                out.println("<script>alert('新闻人物添加失败！'); window.location.href='http://localhost:8080/news_war_exploded/pageperson?rows=2&currentPage=1';</script>");
            }
        }
    }
}
